﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XeBus.BusinessLogic;
using XeBus.Controller;
using XeBus.BusinessObject;

namespace XeBus.Controller
{
    public partial class frmConDuong : Form
    {
        private ConDuongBUS cdBUS = new ConDuongBUS();
        private ConDuong cd = new ConDuong();
        private DataTable dt = new DataTable();
        private TramDungBUS tdBUS = new TramDungBUS();
        //public string ma_con_duong { get; set; }
        private int y;

        public frmConDuong()
        {
            InitializeComponent();
            Load_Con_Duong();
        }

        private void Load_Con_Duong()
        {
            dgvConDuong.AutoGenerateColumns = false;
            dgvConDuong.DataSource=cdBUS.ConDuong();
        }

        private void btThemMoi_Click(object sender, EventArgs e)
        {
            frmThemSua ts = new frmThemSua();
            ts.label1.Text = "Thêm con đường";
            ts.label2.Text = "";
            ts.lblTenLoTrinh.Text = "Tên con đường:";
            ts.lblHuongDi.Text = "Quận/Huyện:";

            ts.ShowDialog();
            Load_Con_Duong();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            frmThemSua ts = new frmThemSua();
            ts.label1.Text = "Sửa con đường: mã con đường ";
            ts.lblTenLoTrinh.Text = "Tên con đường:";
            ts.lblHuongDi.Text = "Quận/Huyện:";
            ts.txtTenLoTrinh.Text = dt.Rows[0]["Ten_Duong"].ToString();
            ts.txtHuongDi.Text = dt.Rows[0]["Quan_Huyen"].ToString();
            ts.label2.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            ts.ShowDialog();
            Load_Con_Duong();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult  dr = MessageBox.Show("Bạn có chắc chắn muốn xóa không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    y = dgvConDuong.CurrentCell.RowIndex;
                    cd.Ma_Con_Duong = dgvConDuong.Rows[y].Cells[0].Value.ToString();
                    cdBUS.XOA(cd.Ma_Con_Duong);
                    MessageBox.Show("Xóa dữ liệu thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("Xóa dữ liệu thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                Load_Con_Duong();
            }
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Hien_Thi(int y)
        {
            string id = dgvConDuong.Rows[y].Cells[0].Value.ToString();
            dt = cdBUS.ConDuongTheoID(id);

            lblMaConDuong.Text = dt.Rows[0]["Ma_Con_Duong"].ToString();
            lblQuan_Huyen.Text = dt.Rows[0]["Quan_Huyen"].ToString();
            lblTenDuong.Text = dt.Rows[0]["Ten_Duong"].ToString();
        }

        private void dgvConDuong_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            y = e.RowIndex;
            Hien_Thi(y);
        }

        private void btSuaTramDung_Click(object sender, EventArgs e)
        {
            try
            {
                frmTramDung td = new frmTramDung();
                td.ma_con_duong = dt.Rows[0]["Ma_Con_Duong"].ToString();
                td.dgvTramDung.AutoGenerateColumns = false;
                td.dgvTramDung.DataSource = tdBUS.TramDung(td.ma_con_duong);
                td.ShowDialog();
            }
            catch (Exception) { }
        }
    }
}
